<?php
declare (strict_types = 1);

namespace app\admin\controller\system;

use app\admin\BaseController;
use app\common\model\system\Log as LogModel;
use think\Exception;

class Log extends BaseController
{
    //中间件
    protected $middleware = [
        'method:ajax' => ['only'=>['list','delete', 'batchDel','clear']],
    ];


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        return view('index');
    }

    /**
     * 数据列表
     */
    public function list(){
        try{
            //获取列表数据
            $data = LogModel::getListData($this->ruleWhere());


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'count' => $data['count'],
            'data'  => $data['data']
        ]);
    }


    /**
     * 显示指定的资源
     * @param $id
     * @return \think\response\View
     * @throws \think\Exception
     */
    public function read(int $id)
    {
        return view('read',[
            'model' => $this->getModel(LogModel::class, $id,['user'])
        ]);
    }



    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id)
    {

        try{
            //获取参数
            $res = $this->getModel(LogModel::class, $id)->delete();

            if ($res === false){
                throw new Exception('删除日志失败.');
            }

        }catch (\Exception $e){

            //Log::error($e->getMessage());
            return json(['code'=>1,'msg'=>$e->getMessage()]);
        }
        //删除成功
        return json(['code'=>0,'msg'=>'删除日志成功']);
    }

    /**
     * 批量删除
     */
    public function batchDel(){
        try{
            $ids = explode(',',trim(request()->param('ids'),','));

            if( $this->batchRule(LogModel::class, (array) $ids) &&
                LogModel::destroy($ids) === false )
            {
                throw new Exception('批量删除日志失败.');
            }

        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }

        return json([
            'code' => 0,
            'msg' => '批量删除日志成功.'
        ]);
    }
    /**
     * 清空操作
     */
    public function clear(){
        try{
            $res = LogModel::destroy(function ($query){
                return true;
            });
            if( $res === false)
            {
                throw new Exception('清空日志失败.');
            }

        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }

        return json([
            'code' => 0,
            'msg' => '清空日志成功.'
        ]);
    }
}
